package com.ungao.tools.generator.create;


import com.ungao.tools.generator.beans.TableInfo;
import com.ungao.tools.generator.builder.TableBuilder;
import com.ungao.tools.generator.connection.DbConnection;
import com.ungao.tools.generator.converter.CaseConverter;
import com.ungao.tools.generator.engine.FreemarkerTemplateEngine;
import com.ungao.tools.generator.engine.TemplateEngine;
import com.ungao.tools.generator.engine.config.TemplateConfig;
import com.ungao.tools.generator.engine.type.EngineType;
import com.ungao.tools.generator.rules.MysqlColumnType;
import com.ungao.tools.generator.util.PathUtil;

import java.sql.Connection;
import java.util.Arrays;

public class DefaultSimpleCreator extends AbstractCreator<TableInfo, String> {

    public DefaultSimpleCreator(String tableName) {
        super(tableName);
    }

    public DefaultSimpleCreator(String names, String alias) {
        super(names, alias);
    }

    @Override
    public DefaultSimpleCreator connection(Connection connection) throws Exception {
        super.tables = new TableBuilder(Arrays.asList(super.names), Arrays.asList(super.alias))
                .create(new DbConnection(connection))
                .handle(MysqlColumnType.getInstance())
                .converter(new CaseConverter())
                .build().get(0);
        return this;
    }

    @Override
    public DefaultSimpleCreator connection(String url, String username, String password, String driver) throws Exception {
        super.tables = new TableBuilder(Arrays.asList(super.names), Arrays.asList(super.alias))
                .create(new DbConnection(url, username, password, driver))
                .handle(MysqlColumnType.getInstance())
                .converter(new CaseConverter())
                .build().get(0);
        return this;
    }

    @Override
    public DefaultSimpleCreator configuration(TemplateConfig configBuilder) throws Exception {
        super.configuration(configBuilder);
        return this;
    }

    @Override
    public void write(EngineType engineType) throws Exception {
        if (EngineType.Freemarker.equals(engineType)) {
            TemplateEngine engine = new FreemarkerTemplateEngine().init(super.configBuilder);
            if (configBuilder.getModulePath() != null && !configBuilder.getModulePath().equals("")) {
                final String modulePath = PathUtil.convertPath(configBuilder.getModulePath());
                String targetPath = PathUtil.concatToPath(PathUtil.createPath(modulePath), tables.getClassName(), configBuilder.getModuleSuffix(), PathUtil.JAVA);
                engine.writer(super.tables, "Model.ftl", targetPath);
            }
            if (configBuilder.getMapperPath() != null && !configBuilder.getMapperPath().equals("")) {
                final String mapperPath = PathUtil.convertPath(configBuilder.getMapperPath());
                String targetPath = PathUtil.concatToPath(PathUtil.createPath(mapperPath), tables.getClassName(), configBuilder.getMapperSuffix(), PathUtil.JAVA);
                engine.writer(super.tables, "Mapper.ftl", targetPath);
            }
            if (configBuilder.getXmlPath() != null && !configBuilder.getXmlPath().equals("")) {
                final String xmlPath = PathUtil.convertPath(configBuilder.getXmlPath());
                String targetPath = PathUtil.concatToPath(PathUtil.createPath(xmlPath), tables.getClassName(), configBuilder.getXmlSuffix(), PathUtil.XML);
                engine.writer(super.tables, "Xml.ftl", targetPath);
            }
            if (configBuilder.getServicePath() != null && !configBuilder.getServicePath().equals("")) {
                final String servicePath = PathUtil.convertPath(configBuilder.getServicePath());
                String targetPath = PathUtil.concatToPath(PathUtil.createPath(servicePath), tables.getClassName(), configBuilder.getServiceSuffix(), PathUtil.JAVA);
                engine.writer(super.tables, "Service.ftl", targetPath);
            }
            if (configBuilder.getImplPath() != null && !configBuilder.getImplPath().equals("")) {
                final String implPath = PathUtil.convertPath(configBuilder.getImplPath());
                String targetPath = PathUtil.concatToPath(PathUtil.createPath(implPath), tables.getClassName(), configBuilder.getServiceSuffix(), configBuilder.getImplSuffix(), PathUtil.JAVA);
                engine.writer(super.tables, "ServiceImpl.ftl", targetPath);
            }
            if (configBuilder.getControlPath() != null && !configBuilder.getControlPath().equals("")) {
                final String controlPath = PathUtil.convertPath(configBuilder.getControlPath());
                String targetPath = PathUtil.concatToPath(PathUtil.createPath(controlPath), tables.getClassName(), configBuilder.getControlSuffix(), PathUtil.JAVA);
                engine.writer(super.tables, "Controller.ftl", targetPath);
            }
        }
    }
}
